package 力扣.双指针.双指针;

public class 子数组最大平均数643 {
    public static void main(String[] args) {
//        int[] ins = new int[]{1,12,-5,-6,50,3};
        int[] ins = new int[]{1,2};
        double maxAverage = findMaxAverage(ins, 2);
        System.out.println(maxAverage);
    }

    /**
     * 使用双指针维持一个‘k’大小的滑动窗口
     * @param nums
     * @param k
     * @return
     */
    public static double findMaxAverage(int[] nums, int k) {
        int max = 0;
        for (int i = 0; i < k; i++) {//先创建长度为k的窗口
            max += nums[i];
        }
        int left = 0;//left:左指针
        int maxTe = max;
        for (int i = k; i < nums.length; i++) {//i:右指针
            maxTe = maxTe - nums[left] + nums[i];
            max = Math.max(max, maxTe);
            left++;
        }
        return (double) max/k;
    }
}
